package com.hzhh123.app05.list;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.hzhh123.app05.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ListActivity extends AppCompatActivity {
    private ListView mListView;
    private Button mButton;
    private List<ListItem> mListItems;
    private int SUCCESS=1;
    private ListItemAdapter mListItemAdapter;
    private Map<Integer,String> map;

    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == SUCCESS){
                mListItemAdapter = new ListItemAdapter(mListItems,ListActivity.this);
                mListView.setAdapter(mListItemAdapter);
                mListItemAdapter.notifyDataSetChanged();
                map = mListItemAdapter.map;
            }
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        initView();
        getData();
    }

    public void initView(){
        mListView=(ListView)findViewById(R.id.list_v);
        mButton=(Button)findViewById(R.id.btn_save);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StringBuffer sb = new StringBuffer(100);
                //遍历map中的key 和 value
                Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<Integer, String> entry = it.next();
                    System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
                    sb.append(entry.getKey()+","+entry.getValue().trim()+";");

                    //设置状态status
                    if (entry.getValue().equals("符合")) {
                        mListItems.get(entry.getKey()).setStatus("1");
                    }
                    if (entry.getValue().equals("不符合")) {
                        mListItems.get(entry.getKey()).setStatus("2");
                    }
                    if (entry.getValue().equals("缺项")) {
                        mListItems.get(entry.getKey()).setStatus("0");
                    }
                    if (entry.getValue().equals("不检查")) {
                        mListItems.get(entry.getKey()).setStatus(null);
                    }
                }
                String data = sb.toString();
                System.out.println(mListItems);
                Toast.makeText(ListActivity.this,data,Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void getData(){
        mListItems = new ArrayList<ListItem>();
        for (int i = 0;i < 20;i++){
            ListItem listItem = new ListItem();
            listItem.setId((i+1)+"");
            listItem.setName("质量检查"+(i+1));
            if(i%3==0){
                listItem.setStatus("0");
            }
            if(i%3==1){
                listItem.setStatus("1");
            }
            if(i%3==2){
                listItem.setStatus("2");
            }

            if(i==2 || i== 4 || i==7){
                listItem.setStatus(null);
            }
            mListItems.add(listItem);
        }
        mHandler.sendEmptyMessage(SUCCESS);
    }
}
